import win32gui as gui
import win32con as con
import win32clipboard as clipboard
from ctypes import windll as dll
from PIL import Image
import os


class MessageType:
    text = 0
    image = 1


class AutoMessage:

    def __init__(self, receiver: str):
        self.hwnd = gui.FindWindow(None, receiver)

    def send_out(self):
        clipboard.OpenClipboard()
        clipboard.EmptyClipboard()

        if self.type == MessageType.text:
            clipboard.SetClipboardData(con.CF_UNICODETEXT, self.message)
        elif self.type == MessageType.image:
            clipboard.SetClipboardData(con.CF_BITMAP, self.message)

        clipboard.CloseClipboard()

        gui.SendMessage(self.hwnd, con.WM_PASTE, 0, 0)
        gui.SendMessage(self.hwnd, con.WM_KEYDOWN, con.VK_RETURN, 0)

    def send_message(self, mType: MessageType, mContent: str):
        self.type = mType
        self.message = mContent

        # Process text.
        if self.type == MessageType.text:
            print('Text detected.')
            pass
        # Process image.
        elif self.type == MessageType.image:
            print('Image detected.')
            Image.open(self.message).save('temp.bmp')
            self.message = dll.user32.LoadImageW(
                0, 'temp.bmp', con.IMAGE_BITMAP, 0, 0, con.LR_LOADFROMFILE)
            os.remove('temp.bmp')

        # Send processed message.
        self.send_out()


if __name__ == '__main__':
    qq = AutoMessage('小号')
    qq.send_message(MessageType.text,
                    'D:\\Pictures\\Saved Pictures\\miku.png')
